<template>
  <div>
    <!-- 查询表单 -->
    <div class="check-form-box">
      <el-form :inline="true" :model="searchModel" class="demo-form-inline">
        <el-form-item label="用户ID">
          <el-input
            v-model.trim="searchModel.userId"
            :formatter="(value) => value.replace(/[^\d]/g, '')"
            placeholder="请输入用户ID"
          />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input
            v-model.trim="searchModel.userMobile"
            :formatter="(value) => value.replace(/[^\d]/g, '')"
            placeholder="请输入手机号码"
          />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input
            v-model.trim="searchModel.userName"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item label="注册日期" class="timeForm">
          <el-date-picker
            v-model="searchModel.createDate"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" color="#ea5b24" @click="searchUser()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="tableData.data" style="width: 100%">
      <el-table-column prop="id" label="用户ID" />
      <el-table-column prop="userName" label="姓名" />
      <el-table-column prop="realName" label="昵称" />
      <el-table-column prop="userMobile" label="手机号码" />
      <el-table-column
        prop="createTime"
        :formatter="rejigTime"
        label="注册时间"
      />
      <!-- <el-table-column width="120" fixed="right" label="操作">
        <template v-slot:default="scope">
          <el-button link type="primary" size="small" @click="revisePassword(scope.row)">修改密码</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- 分页插件 -->
    <div style="text-align: center">
      <el-pagination
        :total="totalNum"
        @current-change="handleCurrentChange"
        background
        :page-size="size"
        layout="prev, pager, next, total"
        :current-page="page"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { findUserApi } from "../../../api/index";
import { ElMessage } from "element-plus";
import { formatDateTime } from "../../../utils/formatDateTime";

// 分页列表数据
let tableData: any = reactive([]);
const totalNum = ref(0);
const page = ref(1);
const size = 8;

// 搜索模型
const searchModel: any = reactive({
  userId: null,
  userName: null,
  userMobile: null,
  createDate: null,
  startTime: null,
  endTime: null,
  page: page,
  size: size,
});

// 翻页
let flipData = reactive({
  page: page,
  size: size,
});
const handleCurrentChange = (val: number) => {
  page.value = val;
  flipData.page = page.value;
  find(flipData);
};

// 搜索用户
const searchUser = () => {
  page.value = 1;
  if (searchModel.createDate) {
    searchModel.startTime = formatDateTime(searchModel.createDate[0]);
    searchModel.endTime = formatDateTime(searchModel.createDate[1]).replace(
      "00:00:00",
      "23:59:59"
    );
  } else {
    searchModel.startTime = null;
    searchModel.endTime = null;
  }
  searchParmSearch();
  find(searchModel);
  flipData = JSON.parse(JSON.stringify(searchModel));
};

// 搜索参数检查
const searchParmSearch = () => {
  Object.keys(searchModel).forEach((key) => {
    if (!searchModel[key]) {
      searchModel[key] = null;
    }
  });
};

// 查询用户(分页)
const find = (searchModel) => {
  findUserApi(searchModel).then((res) => {
    if (res.data.code === 2000) {
      tableData.data = res.data.data.data;
      totalNum.value = res.data.data.total;
    } else {
      ElMessage.error("查询失败");
    }
  });
};

//处理时间格式
const rejigTime = (row) => {
  let strTime = "";
  row.createTime == null ? "" : (strTime = row.createTime.replace(/T/g, " "));
  return strTime;
};

onMounted(() => {
  find(searchModel);
});
</script>

<style scoped>
.timeForm {
  width: 20% !important;
}
</style>
